#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : ApiResponse.py
@Author  : lepal
@Time    : 2024-08-20 14:46
"""
# -*- coding: utf-8 -*-

from flask import make_response, jsonify


class ApiResponse(object):
    """
    统一的json返回格式
    """

    def __init__(self, data, code, msg):
        self.data = data
        self.code = code
        self.msg = msg

    @classmethod
    def success(cls, data=None, code=0, msg=''):
        return jsonify(cls(data, code, msg).to_dict())

    @classmethod
    def error(cls, data=None, code=-1, msg='error'):
        return jsonify(cls(data, code, msg).to_dict())

    def to_dict(self):
        return {
            "code": self.code,
            "msg": self.msg,
            "data": self.data
        }

    # def to_response(self):
    #     response_data = self.to_dict()
    #     response = make_response(jsonify(response_data))
    #     response.headers['Content-Type'] = 'application/json'
    #     return response
